﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using OneoneNine.Data;
using OneoneNine.Models;

namespace OneoneNine.Controllers
{
    public class RejectsController : Controller
    {
        private readonly ApplicationDbContext _context;

        public RejectsController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: Rejects
        public async Task<IActionResult> Index()
        {
            return View(await _context.Reject.ToListAsync());
        }

        // GET: Rejects/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var reject = await _context.Reject
                .FirstOrDefaultAsync(m => m.Id == id);
            if (reject == null)
            {
                return NotFound();
            }

            return View(reject);
        }

        // GET: Rejects/Create
        public IActionResult Create(int param1, string param2, string param3)
        {
            var model = new Reject()
            {
                Id = param1,
                StudentName = param2,
                Reason = param3
            };
            return View("Create", model);
        }

        // POST: Rejects/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,StudentName,Reason,RejectRs,State")] Reject reject)
        {
            if (ModelState.IsValid)
            {
                _context.Add(reject);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index", "Audit");
            }
            return View(reject);
        }


        // GET: Rejects/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var reject = await _context.Reject.FindAsync(id);
            if (reject == null)
            {
                return NotFound();
            }
            return View(reject);
        }

        // POST: Rejects/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,StudentName,Reason,RejectRs,State")] Reject reject)
        {
            if (id != reject.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(reject);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RejectExists(reject.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(reject);
        }

        // GET: Rejects/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var reject = await _context.Reject
                .FirstOrDefaultAsync(m => m.Id == id);
            if (reject == null)
            {
                return NotFound();
            }

            return View(reject);
        }

        // POST: Rejects/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var reject = await _context.Reject.FindAsync(id);
            _context.Reject.Remove(reject);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool RejectExists(int id)
        {
            return _context.Reject.Any(e => e.Id == id);
        }
    }
}
